<?php 
namespace app\admin\controller;

use app\admin\model\PageModel;

/**
 * 单页管理
 */
class Page extends Base
{
	
	public function index()
	{
		$page = new PageModel();

		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");

		if ($type == 1) {
			$where    = [];
			$order    = "page_id desc";
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;

			// 统计
			$sum   = $page->get_count($where);
			// 获取数据
			$list   = $page->get_list_page($where, $order, $fields = "*", $start, $size);

            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $list;

            echo json_encode($data);
            exit;
		}

		return $this->fetch('index');
	}
	
	// 编辑单页
	public function edit()
	{
		$page      = new PageModel();

		// 提交数据处理
		$flag      = input("param.flag/s");
		$page_id   = input("param.page_id/d");

		if (request()->isAjax()) {

			// 获取数据
			$data = input("param.");
			$data['status'] = empty($data['status'])? -1: $data['status'];

			if ($flag == "add") {
				$res  = $page->insert_data($data);
				if (isset($res['msg'])) {
					echo json_encode($res);exit;
				}
			}elseif($flag == "edit"){
				$res  = $page->save_data($data);
				if (isset($res['msg'])) {
					echo json_encode($res);exit;
				}
			}

			echo json_encode(msg_success("编辑成功"));
			exit;
		}


		if ($flag == "add") {
			return $this->fetch('add');

		}elseif($flag == "edit"){
			$data = [];
			// 获取该条数据详情
			$result = $page::get($page_id);
			$data['data'] = $result;

			return $this->fetch('edit', $data);
		}
	}

	// 删除单页
	public function delpage()
	{
		$page   = new PageModel();
		$id     = input("param.page_id/d");
		$ids    = input("param.ids");

		if (!empty($id)) {
			$where = ['page_id' => $id];
		}

		if (!empty($ids)) {
			$ids   = json_decode($ids);
			$where = "page_id IN (".implode(",", $ids).")";
		}

		// 删除数据
		$res = $page->del_data($where);
		if (isset($res['msg'])) {
			echo json_encode($res);
		}else{
			echo json_encode(msg_success("删除成功"));
		}
		exit;
	}

}


 ?>